#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>


int g_child_stop = 0;
int g_parent_run = 0;

void sig_child(int signum)
{
	if( SIGUSR1 == signum )
	{
		int g_child_stop = 1;
	}
}

void sig_parent(int signum)
{
	if( SIGUSR2 == signum )
	{
		int g_parent_run = 1;
	}
}

int main(int argc ,char **argv)
{
	int		pid;
	int		wstatus;

	signal(SIGUSR1,sig_child);
	signal(SIGUSR2,sig_parent);


	if( (pid=fork()) <0 )
	{
		printf("Create child processcfailure:%s\n",strerror(errno));
		return -2;
	}
	else if(pid==0)
	{
		/*子进程在这里运行，并告诉父进程开始运行*/
		printf("Child process start running and send parent a signal\n");
		kill(getppid(),SIGUSR2);//给父进程发信号


		while( !g_child_stop )
		{
			sleep(1);
		}

		printf("Child process receive signal from parent and exit now\n");
		return 0;
	}
	printf("Parent process hangs up until receive signal from child\n");

		while( !g_parent_run )
		{
			sleep(1);
		}
		/*父进程在这里运行，并告诉子进程退出*/
		printf("Parent process start running and send child a signal to exit\n");
		kill(pid,SIGUSR1);

		/*父进程等待子进程退出*/
		wait(&wstatus);
		printf("Parent process wait child process die and exit now\n");

		return 0;
	
}
